package edu.stu.mall.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import edu.stu.mall.databinding.ItemCartListBinding
import edu.stu.mall.databinding.ItemHomeListBinding
import edu.stu.mall.repository.data.CartListData
import edu.stu.mall.repository.data.CartListDataItem
import edu.stu.mall.repository.data.HomeListItemData
import edu.stu.mall.service.loadImage

class CartListAdapter : RecyclerView.Adapter<CartListAdapter.CartListViewHolder>() {

    private var dataList: List<CartListDataItem> = mutableListOf()
    fun setData(list: List<CartListDataItem>?) {
        if (list != null && list.isNotEmpty()) {
            dataList = list
            notifyDataSetChanged()
        }
    }


    class CartListViewHolder(binding: ItemCartListBinding) : RecyclerView.ViewHolder(binding.root) {
        var itemBinding: ItemCartListBinding

        init {
            itemBinding = binding
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CartListViewHolder {

        return CartListViewHolder(
            ItemCartListBinding.inflate(
                LayoutInflater.from(parent.context),
                parent, false
            )
        )
    }

    override fun getItemCount(): Int {
        return dataList.size

    }

    override fun onBindViewHolder(holder: CartListViewHolder, position: Int) {
//        holder.itemBinding.item = dataList[position]
        val data = dataList[position]

        holder.itemBinding.apply {
            item = data
            loadImage(data.productPic, ivPic)
            tvPrice.text = "￥：${
                if (data.price!! % 1 == 0.0) {
                    data.price.toInt()
                } else {
                    data.price.toString()
                }
            }"
//            tvPrice.setTextColor(Color.RED)
            tvQuantity.text = "数量: ${data.quantity}"
            tvProductAttr.text=data.productAttr

        }

    }
}